package com.xw.childhealth.controller;

import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.ChildrenStandard;
import com.xw.childhealth.entity.DataDictionary;
import com.xw.childhealth.service.ChildrenStandardService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-14 10:24
 * @Version: 1.0
 */
@RestController
@RequestMapping("/childrenStandard")
public class ChildrenStandardController {

    @Autowired
    private ChildrenStandardService childrenStandardService;


    /**
     * 查询下拉列表
     *
     * @return
     */
    @PostMapping("/getChildrenStandardParent")
    public ResultVO getChildrenStandardParent() {

        List<ChildrenStandard> result = childrenStandardService.findByParentIdEquals(0);

        return ResultVOUtil.success(result);
    }


    /**
     * 查询下拉列表详细内容
     *
     * @param id
     * @return
     */
    @PostMapping("/getChildrenStandardDetail/{id}")
    public ResultVO findDetail(@PathVariable("id") Integer id) {

        List<ChildrenStandard> result = childrenStandardService.findByParentIdEquals(id);

        return ResultVOUtil.success(result);
    }


    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "value2", required = false) String value2,
                             @RequestParam(value = "value1", required = false) String value1, @RequestParam(value = "nodeId", required = false) Integer nodeId, @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/children-standard", map);
        }
        if (type.equals("toUpd")) {
            ChildrenStandard one = childrenStandardService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            ChildrenStandard one = childrenStandardService.findByIdEquals(nodeId);
            one.setDetail(value2);
            one.setOrderNumber(value1);
            childrenStandardService.save(one);
            return new ModelAndView("/pages/admin/children-standard", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            childrenStandardService.save(new ChildrenStandard().setParentId(addRecordNode).setOrderNumber(value1).setDetail(value2).setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/children-standard", map);
        }
        return new ModelAndView("/pages/admin/children-standard-data-add", map);
    }


    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        ChildrenStandard result = childrenStandardService.findByIdEquals(id);
        if (result != null) {
            childrenStandardService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/children-standard");
    }
}
